<?php
require_once("inc.member.php");

$this_title="$vars[member_title] &raquo; ".__("Placement Paradigm");
$page_title=__("Placement Paradigm");
$content_title=__("Placement Paradigm");

$r_status_d=array("normal"=>__("Normal"),"unverified"=>__("Unverified"),"suspended"=>__("Suspended"),"terminated"=>__("Terminated"));

//#####request to make placement
if($_POST["__req"]){
 //error check and verification
 $position_data=explode("#", $vars["dbr"]["users"]["matrix_position"]);
 $sloti=explode("-", $post_s['slot']);
 $upline_id=$sloti[0];
 $slot=$sloti[1];
 if(0){//!@mysql_num_rows(mysql_query("select * from $db->purchase where status='confirmed' and uid='$post_d[did]'"))){
  $errmsg.=__("You have not activated your account yet and cannot place your downline.")."<br />\n";
 }elseif(!$post_s["did"]){
  $errmsg.=__("Please select a downline first.")."<br />\n";
 }elseif(!$user=get_user_detail_by_id($post_d["did"])){
  $errmsg.=__("Invalid downline selected, please select a valid downline.")."<br />\n";
 }elseif($user["ref"]!=$uid){
  $errmsg.=__("This Member ID '$post_h[did]' is not sponsored by you.")."<br />\n";
 }elseif($user["matrix_upline"]!=0){
  $errmsg.=__("This Member ID '$post_h[did]' has already been placed in the matrix network and placement cannot continue.")."<br />\n";
 }elseif($user["status"]!="normal"){
  $errmsg.="The status for this Member ID '$post_h[did]' is ".$r_status_d[$user["status"]].", only member with status '".$r_status_d['normal']."' can be placed.<br />\n";
 }elseif($user["activated"]!="y"){
  $errmsg.="Member ID '$post_h[did]' is not activated yet, only activated member can be placed.<br />\n";
 }elseif(!$upline_id){
  $errmsg.="Invalid upline selected. Please select a valid upline.<br />\n";
 }elseif(!$upline=get_user_detail_by_id($upline_id)){
  $errmsg.="Invalid upline selected. Please select a valid upline.<br />\n";
 }elseif($upline["activated"]!="y"){
  $errmsg.="Placement ID '$post_h[upline]' is not activated yet, only activated upline can have downline placement.<br />\n";
 }elseif($upline_id!=$uid && !is_group_matrix_downline_of($upline_id, $uid)){
  $errmsg.="Placement ID '$upline_id' does not belong to your downline group.<br />\n";
 }elseif(!$slot){
  $errmsg.="No slot selected. Please select a slot.<br />\n";
 }elseif(!is_numeric($slot)){
  $errmsg.="Invalid slot selected. Please select a valid slot.<br />\n";
 }elseif($slot<=0 || $slot>$position_data[6]){
  $errmsg.="The slot position ranges from 1 to $position_data[6], you have selected $slot.<br />\n";
 }elseif(@mysql_num_rows(mysql_query("select * from $db->users where matrix_upline='$upline_id' and matrix_position='$slot'"))){
  $errmsg.="The selected upline (ID: $upline_id) and slot position ($slot) is already occupied, please select another.<br />\n";
 }

 //place member if no error
 if(!$errmsg){
  $placement_date_q=$user["placement_date"]=="0000-00-00 00:00:00"? ", placement_date='".ndate($vars["system_date_format"])."'" : "";
  if(!mysql_query($sql="update $db->users set matrix_upline='$upline_id', matrix_position='$slot' $placement_date_q where id='$post_d[did]' limit 1")){
   $errmsg.="Error placing the member (ID: $post_h[did]) into the selected matrix position.<br />\n".($vars['debug']? "<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n" : "");
   $critical_error.="Error placing the member (ID: $post_h[did]) into the selected matrix position.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
  }
 }

 if(!$errmsg){
  $msg="The member (ID: $post_h[did]) has been successfully placed in the matrix network.<br />\n";
 }

 $errmsg=$errmsg? format_err($errmsg) : "";
 $msg=$msg? format_msg($msg) : "";
}

//javascript
$jvscript=
"<script type='text/javascript' src='".JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js")."'></script>
<script type='text/javascript'>
jQuery(document).ready(function(j){
 j('form[@name=placement_form]').submit(function(){
  var confirmed=false;
  var did=j('select[@name=did] > option:selected').val();
  if(!did){
   alert('".AddSlashes(__("Please select a downline first."))."');
  }else{
   var confirm_text='".AddSlashes(__("Place this downline (ID: <#uid#>) into the selected upline and slot position. Are you sure?"))."';
   confirm_text=confirm_text.replace(/\<#uid#\>/g, did);
   if(confirm(confirm_text)){
    confirmed=true;
    j('input[@name=submit_btn]').attr('disabled','disabled');
   }
  }
  if(!confirmed){
   return false;
  }
 });
});
</script>";

//get all downlines of uid
$r_downline[0]="";
$r_downline_d[""]=__("Please select a downline");
$t=@mysql_num_rows($r=mysql_query("select * from $db->users where ref='$uid' and matrix_upline='0' order by cdate"));
for($i=0;$i<$t;$i++){
 $this_downline=mysql_fetch_assoc($r);
 $downlines[$this_downline['id']]=$this_downline;
 $r_downline[($i+1)]=$this_downline['id'];
 $r_downline_d[$this_downline['id']]=__("ID").__(":")." ".$this_downline['id'].__(",")." ".__("Name").__(":")." ".$this_downline['name']." ".__("(").$r_status_d[$this_downline['status']].__(",")." ".($this_downline["activated"]=='y'? __("Activated") : __("Inactive")).__(")");
}
$downline_select=build_select($r_downline, $r_downline_d, $post_s["did"], "did", $inputbox_style);


$matrix = build_matrix_network($uid, true, 0, $uid, "$upline_id-$slot", "member");//head id, enable radio, row, highlight uid, selected slot, css prefix
$matrix=($errmsg || $msg? $errmsg.$msg : "")."
<form name='placement_form' method='post' action='$this_file?$_SERVER[QUERY_STRING]'>
<input type='hidden' name='__req' value='1' />
<table class='pbt_table'>
 <tr>
  <td width='180'>".__("Select Downline").__(":")." ".__("*")."</td>
  <td>$downline_select</td>
 </tr>
</table>
$matrix
<div class='padding_height center'>
 <input type='submit' value=\"".__("Place This Member")."\" name='submit_btn' />
</div>
</form>";

$content="<h2>$page_title</h2>$matrix";
           
print format_member_page($content, $this_title, $content_title, $jvscript);
?>